package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static java.time.LocalDateTime.now;

/**
 * @author cx
 * @date 2023/4/3 20:41
 * @Version 1.0
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public Map<String, Object> index() {

        Map<String, Object> result = new HashMap<>();

        // 默认的keyword
        MarketKeyword defaultKeyword = queryDefaultKey();
        result.put("defaultKeyword", defaultKeyword);
        // 是否热门，热门的列表获取
        List<MarketKeyword> hotKeyword = queryHotKey();
        result.put("hotKeywordList", hotKeyword);
        // 搜索的历史记录
        List<MarketSearchHistory> historyKeyword = queryHistories();
        result.put("historyKeywordList", historyKeyword);

        return result;
    }

    /**
     * 查询热门关键词
     *
     * @return
     */
    private List<MarketKeyword> queryHotKey() {
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        keywordExample.or().andIsHotEqualTo(true);
        return marketKeywordMapper.selectByExample(keywordExample);
    }

    /**
     * 查询历史
     *
     * @return
     */
    private List<MarketSearchHistory> queryHistories() {
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.or().andDeletedEqualTo(false);
        List<MarketSearchHistory> historyKeyList = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        List<MarketSearchHistory> newHistoryKeywordList = new ArrayList<>();
        List<String> list = new ArrayList<>();
        for (MarketSearchHistory marketSearchHistory : historyKeyList) {
            String keyword = marketSearchHistory.getKeyword();
            if (!list.contains(keyword)) {
                list.add(keyword);
                newHistoryKeywordList.add(marketSearchHistory);
            }
        }
        Comparator<MarketSearchHistory> comparator = Comparator.comparing(MarketSearchHistory::getUpdateTime);
        List<MarketSearchHistory> collectResult = newHistoryKeywordList.stream().sorted(comparator.reversed()).collect(Collectors.toList());
        return collectResult;
    }

    /**
     * 查询默认关键词
     * @return
     */
    private MarketKeyword queryDefaultKey() {
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        keywordExample.or().andIsDefaultEqualTo(true);
        return marketKeywordMapper.selectOneByExample(keywordExample);


    }

    @Override
    public List<String> helper(String keyword) {
        List<String> result = queryKeyword(keyword);
        return result;
    }

    @Override
    public void clearHistory(Integer userId) {
        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        historyExample.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        marketSearchHistoryMapper.logicalDeleteByExample(historyExample);
    }

    private List<String> queryKeyword(String keyword) {
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        keywordExample.or().andKeywordLike("%" + keyword + "%");
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(keywordExample);
        List<String> result = new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {
            result.add(marketKeyword.getKeyword());
        }
        return result;
    }
}
